package Lee_code;

import java.util.HashSet;

public class Lee_142 {
     class ListNode {
      int val;
      ListNode next;
      ListNode(int x) {
          val = x;
          next = null;
      }
  }
    public ListNode detectCycle(ListNode head) {
        HashSet<ListNode> hashSet = new HashSet<>();
        while (head!=null)
        {
            if(!hashSet.contains(head))
            {
                hashSet.add(head);
            }
            else
            {
                return head;
            }
            head = head.next;
        }
        return null;
    }

}
